using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;
using System.Linq;
using System.Web;
using Helpers;
using Helpers.Extensions;
using Helpers;
using Common.Constants;


namespace HttpRequester.RequestMakers.Base
{
    public abstract class RequestInfo
    {
        public RequestChainContext Context { get; set; }

        //public WebProxy Proxy { get; set; }
        public string Url { get; set; }
        public string ContentType { get; set; }
        public HttpMethod Method { get; set; }
        public Dictionary<string, string> PostMembers { get; set; }
        public Dictionary<string, string> GetMembers { get; set; }
        //public Dictionary<string, string> CustomHeaders { get; set; }
        //public Dictionary<HttpRequestHeader, string> WellknownHeaders { get; set; }
        //public List<Cookie> InputCookies { get; set; }

        public RequestInfo()
        {
            Clear();
        }



        public virtual void Clear()
        {
            // Set default values for properties
            ContentType = HttpContentType.APPLICATION_X_WWW_FORM_URLENCODED;
            Method = HttpMethod.GET;

            // Initialize collection properties
            PostMembers = new Dictionary<string, string>();
            GetMembers = new Dictionary<string, string>();

            // Context Initialization
            Context = new RequestChainContext();
            Context.CommonCustomHeaders = new Dictionary<string, string>();
            Context.CommonWellknownHeaders = new Dictionary<HttpRequestHeader, string>();
            Context.InputCookies = new Dictionary<string,Cookie>();
        }

        public void Copy(RequestInfo ri)
        {
            Url = ri.Url;
            ContentType = ri.ContentType;
            Method = ri.Method;
            PostMembers = ri.PostMembers;
            GetMembers = ri.GetMembers;
            Context = ri.Context;
        }

        public void AddPost(string key, string val)
        {
            PostMembers.AddOrReset(key, val);
        }

        public void LoadPostData(Dictionary<string, string> postMembers)
        {
            if (!CollectionHelper.IsNullOrEmpty(postMembers))
                postMembers.Keys.ToList().ForEach(name =>
                    {
                        string value = postMembers[name];
                        AddPost(name, value);
                    });
        }

        public void AddGet(string key, string val)
        {
            GetMembers.AddOrReset(key, val);
        }

        public void LoadGetData(Dictionary<string, string> getMembers)
        {
            if (!CollectionHelper.IsNullOrEmpty(getMembers))
                getMembers.Keys.ToList().ForEach(name =>
                {
                    string value = getMembers[name];
                    AddGet(name, value);
                });
        }
    }
}